package com.panda.pandawash.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.panda.pandawash.R;
import com.panda.pandawash.application.CDApplication;
import com.panda.pandawash.bean.HttpResultBean;
import com.panda.pandawash.util.HttpUtil;
import com.panda.pandawash.util.WsUtils;
import com.panda.pandawash.view.StatusBarCompat;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by gaowf on 2017/2/9.
 */

public class SafeForgetPsdActivity extends Activity {
    private static String TAG = "API_TAG";
    private int time = 60;
    @ViewInject(R.id.edit_pswd_phone_number_safe_box)
    private EditText phoneNumber;
    private String getPhoneNumber;

    @ViewInject(R.id.edit_psd_sms_number_safe_box)
    private EditText phoneVerifyCode;
    private String getPhoneVerifyCode;

    @ViewInject(R.id.btn_safe_box_psd_return)
    private ImageView returnImg;

    @ViewInject(R.id.btn_psd_next_step_safe_box)
    private Button nextButton;

    @ViewInject(R.id.btn_get_psd_sms_number_safe_box)
    private Button btnGetSMSNumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_psd_safe_box);
        x.view().inject(this);

        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
            StatusBarCompat.compat(this, getResources().getColor(R.color.colorPrimary));
        }

        initRes();


    }

    private void initRes() {

        //phoneNumber.addTextChangedListener(watcher);
        getPhoneNumber = phoneNumber.getText().toString().trim();

        phoneVerifyCode.addTextChangedListener(watcher);
        getPhoneVerifyCode = phoneVerifyCode.getText().toString().trim();
        //手机号码 11 位， 验证码
        if (getPhoneNumber.length() == 11 && getPhoneVerifyCode.length() > 0) {
            nextButton.setBackgroundResource(R.drawable.ic_register_btn);
            nextButton.setTextColor(0xffffffff);
            nextButton.setClickable(true);
        } else {
            nextButton.setBackgroundResource(R.drawable.ic_btn_disable);
            nextButton.setTextColor(0x80ffffff);
            nextButton.setClickable(false);
        }
    }
    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() > 0) {
                nextButton.setBackgroundResource(R.drawable.ic_register_btn);
                nextButton.setClickable(true);
                nextButton.setTextColor(0xffffffff);
            } else {
                nextButton.setBackgroundResource(R.drawable.ic_btn_disable);
                nextButton.setClickable(false);
                nextButton.setTextColor(0x88ffffff);
            }

        }
    };

    @Event(R.id.btn_safe_box_psd_return)
    private void excuteReturn(View v) {
        finish();
    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            time--;
            btnGetSMSNumber.setText("" + time);
            handler.postDelayed(this, 1000);
            if (time <= 0) {
                handler.removeCallbacks(runnable);
                btnGetSMSNumber.setClickable(true);
                btnGetSMSNumber.setText("获取验证码");
            }
        }
    };

    @Event(R.id.btn_get_psd_sms_number_safe_box)
    private void getSMSNumber(View v) {
        getIdCode();
    }

    @Event (R.id.btn_psd_next_step_safe_box)
    private void nextStep(View v) {
        String userAccount = phoneNumber.getText().toString();
        String idCode = phoneVerifyCode.getText().toString();
        if (!WsUtils.isPhoneNumber(userAccount)) {
            showMessage(getString(R.string.registe_incorrect_username));
            return;
        }
        if(!CDApplication.getIntance().getUserInfo().getUseraccount().equals(userAccount)){
            showMessage(getString(R.string.safe_box_forget_psw_account_not_match));
            return;
        }
        if(TextUtils.isEmpty(idCode)){
            Toast.makeText(this, R.string.registe_idcode_empty, Toast.LENGTH_SHORT).show();
            phoneVerifyCode.requestFocus();
            return;
        }
        //判断验证码是否匹配。这里暂时直接跳转
        //Toast.makeText(SafeForgetPsdActivity.this, "点击下一步", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(SafeForgetPsdActivity.this, SafeBoxConfirmActivity.class);
        intent.putExtra("useraccount",userAccount);
        intent.putExtra("idCode",idCode);
        startActivity(intent);
        finish();
    }

    private void getIdCode() {
        String userAccount = phoneNumber.getText().toString();
        if (!WsUtils.isPhoneNumber(userAccount)) {
            showMessage(getString(R.string.registe_incorrect_username));
            return;
        }
        if(!CDApplication.getIntance().getUserInfo().getUseraccount().equals(userAccount)){
            showMessage(getString(R.string.safe_box_forget_psw_account_not_match));
            return;
        }
        btnGetSMSNumber.setClickable(false);
        time = 60;
        runnable.run();

        HttpUtil.getIdCode(new SendIdCodeCallback(), userAccount, userAccount);
    }


    private class SendIdCodeCallback extends AsyncHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, String content) {
            super.onSuccess(statusCode, content);
            Log.d(TAG, "SendIdCodeCallback statusCode = " + statusCode);
            Log.d(TAG, "SendIdCodeCallback onSuccess = " + content);
            if (statusCode == 200) {
                HttpResultBean resultBean = HttpUtil.parserHttpResultInfo(content, "user");
                Log.d(TAG, "resultBean = " + resultBean);
                if (resultBean != null) {
                    String code = resultBean.getCode();
                    String message = resultBean.getMessage();
                    Log.d(TAG, "code = " + code);
                    if(code.equals("0")){
                        showMessage(getString(R.string.registe_idcode_success));
                    }else if(code.equals("99")){
                        showMessage(message);
                    }
                }
            } else {
                showMessage(getString(R.string.registe_idcode_fail));
            }
        }
    }

    private void showMessage(String string) {
        Toast toast = Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }


}
